package model.interactionhandler;

import java.util.HashMap;
import java.util.Map;

import model.entity.Avatar;
import model.entity.BasicEntity;
import model.entity.stats.StatType;
import model.item.Item;
import model.item.TakeableItem;


public class SellHandler extends InteractionHandler{
	
	public SellHandler(String theName) {
		super(theName);
	}
	
	/**
	 * returns the avatars inventory with the prices attached
	 * @param av
	 * @return
	 */
	public Map<Item, Integer> getAvatarInventoryWithPrices(Avatar av){
		Map<Item, Integer> outMap = new HashMap<Item, Integer>();
		for (TakeableItem i : av.getInventoryItems()){
			outMap.put(i, i.getPrice());
		}
		return outMap;
	}
	
	/**
	 * checks if the buyer has enough money for the item
	 * (thus checks if the entity we are selling it to can afford it.)
	 * @param ent
	 * @param it
	 * @return
	 */
	public boolean canBuy(BasicEntity ent, TakeableItem it){
		return ent.getStatVal(StatType.MONEY) >= (double)it.getPrice();
	}
	
	/**
	 * does the actual transaction money and of item
	 * between the avatar and the entity it is selling to
	 * @param av
	 * @param ent
	 * @param it
	 */
	public void sell(Avatar av, BasicEntity ent, TakeableItem it){
	
		double cost = (double)it.getPrice();
		ent.addConstantModifier(StatType.MONEY, -cost);
		av.addConstantModifier(StatType.MONEY, cost);
		
		// Give up the item
		ent.removeItem(it);
		
		// Acquire the Item
		av.addItem(it);
	}
}